package com.wzq.solution;

import com.wzq.solution.node.ListNode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution82 {
    public static ListNode deleteDuplicates(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode p = head;
        ListNode rst = null;
        ListNode rstHead = null;
        
        
        boolean isRep = false;
        while(p != null && p.next != null){
            if(p.val == p.next.val){
                isRep = true;
                p.next = p.next.next;
            }else{
                
                if(isRep){
                   p = p.next;
                }else{
                    if(rstHead == null){
                        rstHead = p;
                        rst = rstHead;
                    }else{
                        rst.next = p;
                        rst = p;
                    }
                    p = p.next;

                    rst.next = null;

                }
                isRep = false;
            }
        }


        if(!isRep){
            if(rstHead == null){
                rstHead = p;
            }else{
                rst.next = p;
            }
        }
        
        
        return rstHead;
        
        
        
    }

    public static void main(String[] args) {
        int[] headArr = new int[]{1};
        ListNode head = new ListNode(headArr[0]);
        ListNode p =head;
        for (int i=1;i<headArr.length; i++){
            p.next = new ListNode(headArr[i]);
            p = p.next;
        }
        deleteDuplicates(head);
    }
}